﻿
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Graphics;
using System.IO;
using Path = Microsoft.Maui.Controls.Shapes.Path;

namespace MauiApp1
{
    public class ToolBarButtonDrawable : IDrawable
    {
        public Color? FillColor { get; set; } = Colors.Black;
        public Color LineColor { get; set; } = Colors.White;
        public bool IsClicked { get; set; } = false;
        public string? Text { get; set; }
        public ToolBarButtonType ToolBarButtonType { get; set; }

        public void Draw(ICanvas canvas, RectF dirtyRect)
        {
            switch (ToolBarButtonType)
            {
                case ToolBarButtonType.Add:
                    DrawAddButton(canvas, dirtyRect);
                    break;
                case ToolBarButtonType.Edit:
                    DrawEditButton(canvas, dirtyRect);
                    break;
                default:
                    break;
            }
        }

        public void DrawAddButton(ICanvas canvas, RectF dirtyRect)
        {
            canvas.SaveState();

            if (IsClicked)
            {
                canvas.SetFillPaint(new SolidPaint(Brush.Red.Color), dirtyRect);
            }
            else
            {
                canvas.SetFillPaint(new SolidPaint(FillColor), dirtyRect);
            }
            canvas.FillCircle(dirtyRect.Center.X, dirtyRect.Center.Y, Math.Min(dirtyRect.Width, dirtyRect.Height) / 2);

            // 设置绘制颜色为透明, 线宽为 5  
            canvas.StrokeColor = LineColor;
            canvas.StrokeSize = Math.Min(dirtyRect.Width, dirtyRect.Height) / 25;

            // 计算十字中心点  
            float centerX = dirtyRect.Center.X;
            float centerY = dirtyRect.Center.Y;

            // 绘制十字  
            // 横线  
            canvas.StrokeLineCap = LineCap.Round;
            canvas.DrawLine(centerX - dirtyRect.Width / 2 * 0.25f, centerY, centerX + dirtyRect.Width / 2 * 0.25f, centerY);
            // 竖线  
            canvas.DrawLine(centerX, centerY - dirtyRect.Height / 2 * 0.25f, centerX, centerY + dirtyRect.Height / 2 * 0.25f);

            // 绘制圆环  
            canvas.DrawCircle(centerX, centerY, Math.Min(dirtyRect.Width, dirtyRect.Height) / 2 * 0.5f);  // 圆环比蓝色圆形半径大10  

            canvas.RestoreState();
        }

        public void DrawEditButton(ICanvas canvas, RectF dirtyRect)
        {
            canvas.SaveState();

            if (IsClicked)
            {
                canvas.SetFillPaint(new SolidPaint(Brush.Red.Color), dirtyRect);
            }
            else
            {
                canvas.SetFillPaint(new SolidPaint(FillColor), dirtyRect);
            }
            canvas.FillRectangle(0, 0, dirtyRect.Width, dirtyRect.Height);
            canvas.StrokeColor = LineColor;
            // 设置画笔粗细  
            canvas.StrokeSize = Math.Min(dirtyRect.Width, dirtyRect.Height) / 25;
            float scale = Math.Min(dirtyRect.Width, dirtyRect.Height) / 100;
            // 设置线条圆头  
            canvas.StrokeLineCap = LineCap.Round;
            //外框
            canvas.DrawLine(54*scale,13*scale,12*scale,13*scale);
            canvas.DrawLine(12*scale,13*scale,12*scale,87*scale);
            canvas.DrawLine(12*scale,87*scale,87*scale,87*scale);
            canvas.DrawLine(87*scale,87*scale,87*scale,46*scale);
            //笔                     
            canvas.DrawLine(76*scale,11*scale,88*scale,22*scale);
            canvas.DrawLine(88*scale,22*scale,51*scale,59*scale);
            //笔尖                    
            canvas.DrawLine(51*scale,59*scale,37*scale,63*scale);
            canvas.DrawLine(37*scale,63*scale,40*scale,47*scale);
            //收尾                  
            canvas.DrawLine(40*scale,47*scale,76*scale,11*scale);
        }
    }
}
